/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 */

#include "int_log.h"

void clear_log_yes(void)
{
	evas_object_del(warn_win);
	clear_log_opim();
	clear_log_db();
	elm_genlist_clear(log_list);
}

void clear_log_no(void)
{
	evas_object_del(warn_win);
}

void clear_log(void *data, Evas_Object *obj, void *event_info)
{
	popup_warning("Are You sure you want", "to clear this Log?", clear_log_yes, clear_log_no);
}

char *log_label_get(const void *data, Evas_Object *obj, const char *part)
{
	_Log *p_log= (_Log*)data;
	int min, sec;
	char tystr[255], *ty=NULL;
	if (!strcmp(part, "elm.text")) {
		if (strcmp(p_log->name, "") != 0) strcpy(tystr, p_log->name);
		else strcpy(tystr, p_log->num);
	}
	else if (!strcmp(part, "elm.text.sub")) {
		if(p_log->dur == 0) strcpy(tystr, p_log->timestamp);
		else {
			min = p_log->dur/60;
			sec = p_log->dur - (min * 60);
			sprintf(tystr, "%s                  %d:%02d", p_log->timestamp, min, sec);
		}
	}
	ty = strdup(tystr);
	return (ty);
}

Evas_Object *log_icon_get(const void *data, Evas_Object *obj, const char *part)
{
	_Log *p_log= (_Log*)data;
	char buf[PATH_MAX], buf1[PATH_MAX], buf2[PATH_MAX];
	
	if (!strcmp(part, "elm.swallow.icon"))
	{
		Evas_Object *ic = elm_icon_add(obj);
		Evas_Object *ic1 = elm_icon_add(obj);
		Evas_Object *ic2 = elm_icon_add(obj);
		
		snprintf(buf, sizeof(buf), "/usr/share/launcher/in_arrow.png");
		snprintf(buf1, sizeof(buf1), "/usr/share/launcher/out_arrow.png");
		snprintf(buf2, sizeof(buf2), "/usr/share/launcher/missed_arrow.png");
		elm_icon_file_set(ic, buf, NULL);
		elm_icon_file_set(ic1, buf1, NULL);
		elm_icon_file_set(ic2, buf2, NULL);
		evas_object_size_hint_aspect_set(ic, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
		evas_object_size_hint_aspect_set(ic1, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
		evas_object_size_hint_aspect_set(ic2, EVAS_ASPECT_CONTROL_VERTICAL, 1, 1);
		if (p_log->icon == 1) return ic;
		if (p_log->icon == 2) return ic1;
		if (p_log->icon == 3) return ic2;
		else return NULL;
	}
	return NULL;
}

Eina_Bool log_state_get(const void *data, Evas_Object *obj, const char *part)
{
   return 0;
}

void log_del (const void *data, Evas_Object *obj)
{
}

void log_app(void *data, Evas_Object *obj, void *event_info)
{
	Evas_Object *log_win, *bg, *vbox, *hbox, *toolbar, *button, *ic;
	int i;
	char label[5][255], buf[255], *tystr, *log_type;
	static Winlist wl;

	log_type = (char *)data;
	log_win = elm_win_add(NULL, "log", ELM_WIN_BASIC);
	elm_win_title_set(log_win, "Call Log");
	elm_win_autodel_set(log_win, 1);
	
	//add background
	bg = elm_bg_add(log_win);
	elm_win_resize_object_add(log_win, bg);
	evas_object_size_hint_weight_set(bg, 1.0, 1.0);
	evas_object_show(bg);

	//add vbox 
	vbox = elm_box_add(log_win);
	elm_win_resize_object_add(log_win, vbox);
	evas_object_size_hint_weight_set(vbox, 1.0, 1.0);
	evas_object_show(vbox);

	//add a toolbar
	toolbar = elm_toolbar_add(log_win);
	evas_object_size_hint_weight_set(toolbar, 1.0, 0.0);
	evas_object_size_hint_align_set(toolbar, -1.0, 1.0);
	elm_scroller_bounce_set(toolbar, 0, 0);
	elm_box_pack_end(vbox, toolbar);
	evas_object_show(toolbar);

	//add toolbar items
	sprintf(label[0], "Received");
	sprintf(label[1], "Outgoing");
	sprintf(label[2], "Missed");
	sprintf(label[3], "All");

	for(i=0; i<4; i++) {
		tystr = strdup(label[i]);
		ic = elm_icon_add(log_win);
		sprintf(buf, "%s/%s.png", "/usr/share/launcher", label[i]);
		elm_icon_file_set(ic, buf, NULL);
		if (strcmp(label[i], log_type) == 0) log_tb_item = elm_toolbar_item_add(toolbar, ic, tystr, show_log, (char *) tystr);
		else elm_toolbar_item_add(toolbar, ic, tystr, show_log, (char *)tystr);
	}
	
	//add list to vbox now
	log_list = elm_genlist_add(log_win);
	elm_genlist_bounce_set(log_list, 0, 0);
	elm_genlist_always_select_mode_set(log_list, 1);
	evas_object_size_hint_weight_set(log_list, 1.0, 1.0);
	evas_object_size_hint_align_set(log_list, -1.0, -1.0);
	elm_list_multi_select_set(log_list, 0);
	elm_box_pack_end(vbox, log_list);
	evas_object_show(log_list);
	strcpy(act_da.type, "log");
	act_da.li = log_list;
	evas_object_smart_callback_add(log_list, "selected", show_actions, &act_da);

	//genlist class defs
	itc_log.item_style     	= "double_label";
	itc_log.func.label_get 	= log_label_get;
	itc_log.func.icon_get  	= log_icon_get;
	itc_log.func.state_get 	= log_state_get;
	itc_log.func.del      	= log_del;
	
	//add hbox to vbox
	hbox = elm_box_add(log_win);
	elm_box_horizontal_set(hbox, 1);
	elm_box_homogenous_set(hbox, 1);
	evas_object_size_hint_weight_set(hbox, 1.0, 0.0);
	evas_object_size_hint_align_set(hbox, -1.0, 0.0);
	elm_box_pack_end(vbox, hbox);
	evas_object_show(hbox);
	
	//add button 
	/*button = elm_button_add(log_win);
	elm_button_label_set(button, "Actions");
	evas_object_size_hint_weight_set(button, 1.0, 1.0);
	evas_object_size_hint_align_set(button, -1.0, -1.0);
	elm_box_pack_end(hbox, button);
	evas_object_show(button);
	evas_object_smart_callback_add(button, "clicked", show_actions, log_list);*/

	//add clear button
	button = elm_button_add(log_win);
	elm_button_label_set(button, "Clear Log");
	evas_object_size_hint_weight_set(button, 1.0, 1.0);
	evas_object_size_hint_align_set(button, -1.0, -1.0);
	elm_box_pack_end(hbox, button);
	evas_object_show(button);
	evas_object_smart_callback_add(button, "clicked", (void *)clear_log, NULL);
	
	//add cancel button 
	button = elm_button_add(log_win);
	elm_button_label_set(button, "Done");
	evas_object_size_hint_weight_set(button, 1.0, 1.0);
	evas_object_size_hint_align_set(button, -1.0, -1.0);
	elm_box_pack_end(hbox, button);
	evas_object_show(button);
	wl.win = log_win;
	wl.li = NULL;
	evas_object_smart_callback_add(button, "clicked", cancel_button_clicked, &wl);
	
	// make window full screen
	evas_object_resize(log_win, 480, 640);
	evas_object_show(log_win);
	elm_toolbar_item_select(log_tb_item);
}
